using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using ARF.Web.Fields;
using System.ComponentModel;
using System.Web.UI;

namespace ARF.Web.Controls
{
    [ToolboxData("<{0}:XslSelector runat=\"server\" />"), ToolboxItemFilter("ARF")]
    public class XslSelector : WebControl
    {
        private string _xslPath = "XSL Library";

        public string XslPath
        {
            get { return _xslPath; }
            set { _xslPath = value; }
        }
        private ListBox _xslNames;

        public string XslName
        {
            get 
            {
                EnsureChildControls();

                if (SPContext.Current.FormContext.FormMode == Microsoft.SharePoint.WebControls.SPControlMode.Edit)
                    return _xslNames.SelectedValue;

               return ARFProperties.Current.GetStringProperty("XslSelector: " + ID);
            }
            set 
            {
                ARFProperties.Current.SetProperty("XslSelector: " + ID, value);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            EnsureChildControls();
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (string.IsNullOrEmpty(ID))
                throw new Exception("ID must be specified for XslSelector");

            if (Page.IsPostBack && ARFProperties.Current.IsEditingPostback)
            {
                XslName = _xslNames.SelectedValue;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (SPContext.Current.FormContext.FormMode == Microsoft.SharePoint.WebControls.SPControlMode.Edit)
            {
                _xslNames = new ListBox();
                Controls.Add(_xslNames);

                SPWeb oWeb = GetWebFromPath(); 
                string sPath = ((XslPath.StartsWith("/")) ? XslPath.Substring(1) : XslPath);

                SPFolder oFolder = oWeb.GetFolder(sPath);

                string sValue = ARFProperties.Current.GetStringProperty("XslSelector: " + ID);

                AddFilesToListbox(oFolder, sValue);

                _xslNames.AutoPostBack = true;
            }
        }

        protected void AddFilesToListbox(SPFolder oFolder, string sValue)
        {
            foreach (SPFile oFile in oFolder.Files)
            {
                ListItem oItem = new ListItem(((string.IsNullOrEmpty(oFile.Title)) ? oFile.Name : oFile.Title), oFile.Name);
                if (oItem.Value == sValue) oItem.Selected = true;
                _xslNames.Items.Add(oItem);
            }
        }

        protected virtual SPWeb GetWebFromPath()
        {
            SPWeb oWeb = SPContext.Current.Site.RootWeb;

            if (!XslPath.StartsWith("/")) oWeb = SPContext.Current.Web;
            return oWeb;
        }
    }
}
